package com.neulogistics.core.customercenter.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blackto.lib.mediat.core.IMediator;
import com.neulogistics.core.customercenter.api.application.command.CreateCustomerCommand;
import com.neulogistics.core.customercenter.api.application.command.CustomerIdCommand;
import com.neulogistics.core.customercenter.api.application.command.UpdateCustomerCommand;
import com.neulogistics.core.customercenter.api.application.query.CustomerQuery;
import com.neulogistics.core.customercenter.api.application.query.ICustomerQueryService;
import com.neulogistics.core.customercenter.api.application.viewmodel.CustomerViewModel;
import com.neulogistics.core.customercenter.domain.repository.ICustomerRepository;
import com.neulogistics.core.customercenter.domain.service.ICustomerDomainService;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import com.blackto.lib.eventbus.abstraction.IEventBus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;

@RestController
@Api(tags = "客户管理")
@RequestMapping("/customerCenter/customer")
public class CustomerController {

    @Resource
    ICustomerRepository customerRepository;

    @Resource
    ICustomerQueryService customerQueryService;

    @Resource
    IMediator mediator;


    @ApiOperation("创建客户")
    @PostMapping("/create")
    public BaseResponse<String> createCustomer(@RequestBody @Validated @NotNull CreateCustomerCommand command){
        // 已校验数据格式

        return mediator.send(command);
    }

    @ApiOperation("删除客户")
    @PostMapping("/delete")
    public BaseResponse<String> deleteCustomer(@RequestBody @Positive @NotNull CustomerIdCommand command){
        // 已校验数据格式

        customerRepository.deleteById(command.customerId);

        // 返回
        return ResultUtils.success("删除成功！");
    }

    @ApiOperation("修改客户信息")
    @PostMapping("/update")
    public BaseResponse<String> updateCustomer(@RequestBody @Validated @NotNull UpdateCustomerCommand command){
        // 已校验数据格式

        return mediator.send(command);
    }


    @ApiOperation("根据id查询客户信息")
    @GetMapping("/get/{id}")
    public BaseResponse<CustomerViewModel> get(@PathVariable @Positive @NotNull Integer id){
        // 已校验数据格式
        // 查询
        CustomerViewModel customerViewModel = customerQueryService.getCustomerById(id);
        // 修改成功，返回
        return ResultUtils.success(customerViewModel);
    }

    @ApiOperation("根据条件分页查询客户信息")
    @PostMapping("/page")
    public BaseResponse<Page<CustomerViewModel>> getCustomerByPage(@RequestBody @Validated @NotNull CustomerQuery customerQuery){
        // 已校验数据格式
        // 查询
        Page<CustomerViewModel> page = customerQueryService.getCustomerByPage(customerQuery);
        // 修改成功，返回
        return ResultUtils.success(page);
    }

}
